package entity;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import utils.DockerManager;
import view.DockerContainerView;

public class ServerDockerContainterProcess {
    private int state = 0; // 进程状态 0:未运行 1:运行中
    private String archiveID;
    private String workPath;
    private String containterID;
    private boolean inited = false;
    private int playerNum = 0;
    private String tags = "";
    private Integer memery_limit = -1;
    private Integer swap_limit = -1;
    private Float cpu_limit = (float) -1.0;
    private LocalDateTime lastHaveNoPlayerTime;

    public LocalDateTime getLastHaveNoPlayerTime() {
        return lastHaveNoPlayerTime;
    }


    public void setLastHaveNoPlayerTime(LocalDateTime lastHaveNoPlayerTime) {
        this.lastHaveNoPlayerTime = lastHaveNoPlayerTime;
    }


    BlockingQueue<String> inputLineQ = new ArrayBlockingQueue<>(128);
    // BlockingQueue<String> outputLineQ = new ArrayBlockingQueue<>(128);
    // BlockingQueue<String> errorputLineQ = new ArrayBlockingQueue<>(128);

    public ServerDockerContainterProcess(
        String archiveID, String containterID, String workPath,
        Integer memery_limit, Integer swap_limit, Float cpu_limit
    ) {
        this.archiveID = archiveID;
        this.containterID = containterID;
        this.workPath = workPath;
        if (memery_limit != null) this.memery_limit = memery_limit;
        if (swap_limit != null) this.swap_limit = swap_limit;
        if (cpu_limit != null) this.cpu_limit = cpu_limit;
    }


    public ServerDockerContainterProcess(String archiveID, String containterID, String workPath) {
        this.archiveID = archiveID;
        this.containterID = containterID;
        this.workPath = workPath;
    }

    public void setInited(boolean inited) {
        this.inited = inited;
    }


    public ServerDockerContainterProcess() {
    }

    public void setTag(String tag) {
        this.tags = tag;
    }

    public String getTag() {
        return this.tags;
    }

    public int getState() {
        DockerManager dockerManager = new DockerManager();
        DockerContainerView dockerContainerView = dockerManager.getContainerState(containterID);

        if (dockerContainerView.Running.equals(1)) return 1;
        return 0;
    }

    public String getArchiveID() {
        return archiveID;
    }

    public String getWorkPath() {
        return workPath;
    }

    public String getContainterID() {
        return containterID;
    }

    public void setState(int state) {
        this.state = state;
    }

    public void setArchiveID(String archiveID) {
        this.archiveID = archiveID;
    }

    public void setWorkPath(String workPath) {
        this.workPath = workPath;
    }

    public void setContainterID(String containterID) {
        this.containterID = containterID;
    }

    public boolean isInited() {
        return this.inited;
    }

    public int getPlayerNum() {
        return playerNum;
    }

    public void setPlayerNum(int playerNum) {
        this.playerNum = playerNum;
    }


    public Integer getMemery_limit() {
        return memery_limit;
    }


    public void setMemery_limit(Integer memery_limit) {
        this.memery_limit = memery_limit;
    }


    public Integer getSwap_limit() {
        return swap_limit;
    }


    public void setSwap_limit(Integer swap_limit) {
        this.swap_limit = swap_limit;
    }


    public Float getCpu_limit() {
        return cpu_limit;
    }


    public void setCpu_limit(Float cpu_limit) {
        this.cpu_limit = cpu_limit;
    }
}
